﻿#region Copyright
// Copyright (c) 2011-2012 RaisingForce Team (Alikin Sergey)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using RaisingForce.Raise.Language.Rules;

namespace RaisingForce.Raise.Language.Model
{
    public abstract class GlyphContainer<T> :
        IGlyphContainer<T>
        where T : Glyph
    {
        #region Constructors

        protected GlyphContainer(bool glyphIsSingleton)
        {
            this.glyphIsSingleton = glyphIsSingleton;
        }

        #endregion

        #region Private Declarations

        private readonly bool glyphIsSingleton;

        private readonly GlyphCollection<T> items =
            new GlyphCollection<T>();

        [ContractInvariantMethod]
        private void ObjectInvariant()
        {
            Contract.Invariant(this.GlyphIsSingleton && this.items.Count == 1 || !this.GlyphIsSingleton);
        }

        #endregion

        #region Protected Declarations

        protected void AddGlyph(T item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            Contract.EndContractBlock();
            this.items.Add(item);
        }

        [Pure]
        protected bool ContainsGlyphValue(string glyphValue)
        {
            Contract.Requires(GlyphRules.ValueIsValid(glyphValue));
            return this.items.Contains(glyphValue);
        }

        [Pure]
        protected bool ContainsGlyph(T item)
        {
            return this.items.Contains(item);
        }

        #endregion

        #region IGlyphContainer<T> Members

        public abstract string GlyphTypeName { get; }

        public virtual bool ValueIsAllowed(string value)
        {
            return this.ContainsGlyphValue(value);
        }

        public bool GlyphIsSingleton
        {
            get { return this.glyphIsSingleton; }
        }

        public Type GlyphType
        {
            get { return typeof(T); }
        }

        public T GetGlyph(string value)
        {
            return this.items[value];
        }

        public T GetSingletonGlyph()
        {
            return this.items[0];
        }

        Glyph IGlyphContainer.GetGlyph(string value)
        {
            return this.GetGlyph(value);
        }

        Glyph IGlyphContainer.GetSingletonGlyph()
        {
            return this.GetSingletonGlyph();
        }

        public IEnumerator<T> GetEnumerator()
        {
            return this.items.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        public IList<string> GetAllowedGlyphValues()
        {
            return this.items
                .Select(glyph => glyph.Value)
                .ToArray();
        }

        #endregion
    }
}
